﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Hidistro.Entities.Commodities;
    using wnxd_feature;
    using Hidistro.Core;
    using System.Text;

    [ParseChildren(true)]
    public class VProductList_new : VshopTemplatedWebControl
    {
        private int categoryId;
        private HtmlGenericControl ctitle;
        private HtmlGenericControl clist;
        private HtmlGenericControl clist2;
        private HtmlGenericControl flist;
        private VshopTemplatedRepeater rptProducts;
        private HtmlInputHidden txtTotalPages;
        private HtmlGenericControl i_xl, i_jg, wnxd_px;
        private HtmlAnchor a_qb;
        protected override void AttachChildControls()
        {
            int.TryParse(this.Page.Request.QueryString["CategoryId"], out this.categoryId);
            CategoryInfo category = CategoryBrowser.GetCategory(this.categoryId);
            if (category == null) PageTitle.AddSiteNameTitle("搜索页");
            else
            {
                if (category.ParentCategoryId == 0) this.Page.Response.Redirect("/Vshop/SubPage.aspx?categoryId=" + category.CategoryId);
                PageTitle.AddSiteNameTitle(category.Name);
            }
            this.ctitle = (HtmlGenericControl)this.FindControl("ctitle");
            this.clist = (HtmlGenericControl)this.FindControl("clist");
            this.clist2 = (HtmlGenericControl)this.FindControl("clist2");
            this.flist = (HtmlGenericControl)this.FindControl("flist");
            this.rptProducts = (VshopTemplatedRepeater)this.FindControl("rptProducts");
            this.txtTotalPages = (HtmlInputHidden)this.FindControl("txtTotal");
            this.i_xl = (HtmlGenericControl)this.FindControl("i_xl");
            this.i_jg = (HtmlGenericControl)this.FindControl("i_jg");
            this.wnxd_px = (HtmlGenericControl)this.FindControl("wnxd_px");
            this.a_qb = (HtmlAnchor)this.FindControl("a_qb");
            if (!this.Page.IsPostBack)
            {
                string keywords = this.Page.Request.QueryString["keyWord"];
                if (category == null)
                {
                    if (string.IsNullOrEmpty(keywords)) base.GotoResourceNotFound("无法找到该分类");
                    this.ctitle.InnerText = "搜索页";
                    this.a_qb.InnerHtml = "<span>默认</span>";
                    this.a_qb.HRef = "/Vshop/ProductList.aspx?keyWord=" + this.Escape(keywords);
                    List<CategoryInfo> list = CategoryBrowser.GetMaxMainCategories(0x3e8) as List<CategoryInfo>;
                    for (int i = 0; i < list.Count; i++) this.clist.InnerHtml += string.Format("<li><a href=\"/Vshop/SubPage.aspx?categoryId={0}\">{1}</a></li>", list[i].CategoryId, list[i].Name);
                    this.clist.InnerHtml += "<li><a href=\"/Vshop/CategoryPage.aspx\">全部分类</a></li>";
                }
                else
                {
                    this.ctitle.InnerText = category.Name;
                    this.a_qb.HRef = "/Vshop/ProductList.aspx?categoryId=" + this.categoryId;
                    List<CategoryInfo> list = CategoryBrowser.GetMaxSubCategories(category.ParentCategoryId.Value, 0x3e8) as List<CategoryInfo>;
                    for (int i = 0; i < list.Count; i++) this.clist.InnerHtml += string.Format("<li><a href=\"/Vshop/ProductList.aspx?categoryId={0}\">{1}</a></li>", list[i].CategoryId, list[i].Name);
                    this.clist.InnerHtml += "<li><a href=\"/Vshop/CategoryPage.aspx\">全部分类</a></li>";
                    for (int i = 0; i < list.Count; i++) this.clist2.InnerHtml += string.Format("<li{2}><a href=\"/Vshop/ProductList.aspx?categoryId={0}\">{1}</a></li>", list[i].CategoryId, list[i].Name, list[i].CategoryId == this.categoryId ? " class=\"bjbs\"" : "");
                    this.clist2.InnerHtml += "<li><a href=\"/Vshop/CategoryPage.aspx\">全部分类</a></li>";
                }
                if (!string.IsNullOrEmpty(keywords)) keywords = DataHelper.CleanSearchString(Globals.HtmlEncode(Globals.UrlDecode(keywords)));
                int feature;
                int.TryParse(this.Page.Request.QueryString["feature"], out feature);
                string[] features = new feature().GetFeature(this.categoryId);
                if (features.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("<span><a href=\"{0}\" class=\"wnxd_xz{1}\"><font>全部特点</font><i></i></a></span>", this.CreateUrl("feature", ""), feature == 0 ? " cur" : "");
                    for (int i = 0; i < features.Length; i++) if (!string.IsNullOrEmpty(features[i])) sb.AppendFormat("<span><a href=\"{0}\" class=\"wnxd_xz{1}\"><font>{2}</font><i></i></a></span>", this.CreateUrl("feature", (i + 1).ToString()), feature == i + 1 ? " cur" : "", features[i]);
                    this.flist.InnerHtml = sb.ToString();
                }
                int num2;
                string SortBy = "ShowSaleCounts", SortOrder = "desc";
                if (int.TryParse(this.Page.Request.QueryString["rank"], out num2))
                {
                    switch (num2)
                    {
                        case 1:
                            {
                                SortBy = "ShowSaleCounts";
                                SortOrder = "desc";
                                this.i_xl.Attributes["class"] = "desc";
                                wnxd_px.InnerText = "销量从高到低";
                                break;
                            }
                        case 2:
                            {
                                SortBy = "ShowSaleCounts";
                                SortOrder = "asc";
                                this.i_xl.Attributes["class"] = "asc";
                                wnxd_px.InnerText = "销量从低到高";
                                break;
                            }
                        case 3:
                            {
                                SortBy = "SalePrice";
                                SortOrder = "desc";
                                this.i_jg.Attributes["class"] = "desc";
                                wnxd_px.InnerText = "价格从高到低";
                                break;
                            }
                        case 4:
                            {
                                SortBy = "SalePrice";
                                SortOrder = "asc";
                                this.i_jg.Attributes["class"] = "asc";
                                wnxd_px.InnerText = "价格从高到低";
                                break;
                            }
                    }
                }
                else
                {
                    this.i_xl.Attributes["class"] = "desc";
                    wnxd_px.InnerText = "销量从高到低";
                }
                int num;
                if (!int.TryParse(this.Page.Request.QueryString["page"], out num)) num = 1;
                if (!int.TryParse(this.Page.Request.QueryString["size"], out num2)) num2 = 20;
                int num3;
                this.rptProducts.DataSource = ProductBrowser.GetProducts(null, this.categoryId == 0 ? null : new int?(this.categoryId), keywords, feature, num, num2, out num3, SortBy, SortOrder);
                this.rptProducts.DataBind();
                this.txtTotalPages.SetWhenIsNotNull(num3.ToString());
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-VProductList_new.html";
            }
            base.OnInit(e);
        }
        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pageindex="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }
        private string Escape(string s)
        {
            StringBuilder sb = new StringBuilder();
            byte[] ba = System.Text.Encoding.Unicode.GetBytes(s);
            for (int i = 0; i < ba.Length; i += 2)
            {
                if (ba[i + 1] == 0)
                {
                    if ((ba[i] >= 48 && ba[i] <= 57) || (ba[i] >= 64 && ba[i] <= 90) || (ba[i] >= 97 && ba[i] <= 122) || (ba[i] == 42 || ba[i] == 43 || ba[i] == 45 || ba[i] == 46 || ba[i] == 47 || ba[i] == 95)) sb.Append(Encoding.Unicode.GetString(ba, i, 2));
                    else
                    {
                        sb.Append("%");
                        sb.Append(ba[i].ToString("X2"));
                    }
                }
                else
                {
                    sb.Append("%u");
                    sb.Append(ba[i + 1].ToString("X2"));
                    sb.Append(ba[i].ToString("X2"));
                }
            }
            return sb.ToString();
        }
    }
}

